package com.yonyou.pmclouds.projecttype.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleDeleteBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.EnableStatusConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.ref.itf.ReferenceChecker;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import com.yonyou.pmclouds.projecttype.entity.ProjectTypeVO;
import com.yonyou.pmclouds.projecttype.mapper.ProjectTypeMapper;
import com.yonyou.pmclouds.projecttype.process.ProjectTypeDeleteCheckProcessor;
import com.yonyou.pmclouds.projecttype.process.ProjectTypeExtendCheckProcessor;
import com.yonyou.pmclouds.projecttype.process.ProjectTypeUpdateCheckProcessor;
import com.yonyou.pmclouds.projecttype.rmiitf.ProjectTypeMaintain;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.UUID;

@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = ProjectTypeMaintain.class)
public class ProjectTypeMaintainImpl implements ProjectTypeMaintain {
    /**
     * 更新的字段 todo 191015先还原成只有nc同步的情况
     */
    private static final String[] UPDATE_FIELDS = {ProjectTypeVO.SCODE, ProjectTypeVO.SNAME,ProjectTypeVO.ENABLE_STATUS,
            CommonFieldConst.MODIFIER, CommonFieldConst.MODIFYTIME};
    private static final String[] UPDATE_STATUS = {
            ProjectTypeVO.ENABLE_STATUS, CommonFieldConst.MODIFIER, CommonFieldConst.MODIFYTIME};

    @Autowired
    private ProjectTypeMapper mapper;
    @Autowired
    private ReferenceChecker referenceChecker;

    @Override
    public ProjectTypeVO insert(ProjectTypeVO projectTypeVO) throws BusinessException {
        projectTypeVO.setPkTenant(RuntimeEnvironment.getTenantId());
        SingleInsertBpTemplate<ProjectTypeVO> bp = new SingleInsertBpTemplate<>(mapper);
        // 更新审计信息
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<ProjectTypeVO>());
        //补全VO属性值
        fill(projectTypeVO);
        //检验编码和名称是否存在
        bp.addBeforeProcessor(new ProjectTypeExtendCheckProcessor(mapper));
        return bp.insert(projectTypeVO);
    }
    public void fill(ProjectTypeVO projectTypeVO){
        projectTypeVO.setPkProjectType(UUID.randomUUID().toString());
    }

    @Override
    public String delete(String pkProjectType, String changeFlag) throws BusinessException {
        ProjectTypeVO projectTypeVO = new ProjectTypeVO();
        projectTypeVO.setPkTenant(RuntimeEnvironment.getTenantId());
        projectTypeVO.setChangeFlag(changeFlag);
        projectTypeVO.setPkProjectType(pkProjectType);
        SingleDeleteBpTemplate<ProjectTypeVO> bp = new SingleDeleteBpTemplate<>(mapper);
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ProjectTypeVO>());
        //仅支持末级数据删除,删除时检验是否被单据引用
        bp.addBeforeProcessor(new ProjectTypeDeleteCheckProcessor(mapper,referenceChecker));
        bp.delete(projectTypeVO);
        return projectTypeVO.getPkProjectType();
    }

    @Override
    public ProjectTypeVO update(ProjectTypeVO projectTypeVO) throws BusinessException {
        SingleUpdateBpTemplate<ProjectTypeVO> bp=null;
            bp= new SingleUpdateBpTemplate<>(mapper, UPDATE_FIELDS);
            //更新审计信息
            bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ProjectTypeVO>());
            //检验唯一性 todo 先去掉这个校验是有问题的
            //bp.addBeforeProcessor(new ProjectTypeUpdateCheckProcessor(mapper));
        return bp.update(projectTypeVO);
    }

    @Override
    public ProjectTypeVO enable(ProjectTypeVO projectTypeVO) throws BusinessException {
        //判断父节点是否为开启状态
        if (projectTypeVO.getPkParent()!=null&&!isEnable(projectTypeVO)){
            throw new BusinessException("所选数据的父节点为停用状态，请先启用父节点。");
        }
        SingleUpdateBpTemplate<ProjectTypeVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<ProjectTypeVO>());
        projectTypeVO.setEnableStatus((int) EnableStatusConst.ENABLE);
        projectTypeVO.setPkTenant(RuntimeEnvironment.getTenantId());
        bp.update(projectTypeVO);
        if (projectTypeVO.getPkParent()!=null){
            ProjectTypeVO enable = mapper.isEnable(projectTypeVO.getPkParent(), projectTypeVO.getPkTenant());
            projectTypeVO.setPcode(enable.getScode());
            //设置父节点的字码，前台展示用
        }

        return projectTypeVO;
    }

    @Override
    public ProjectTypeVO[] disEnable(ProjectTypeVO projectTypeVO) throws BusinessException  {
        projectTypeVO.setPkTenant(RuntimeEnvironment.getTenantId());
        //查询出该节点下的所有启用的子节点
        ProjectTypeVO[] projectTypeVOS = mapper.queryAllEnable(projectTypeVO.getScode(), projectTypeVO.getPkTenant(), EnableStatusConst.ENABLE);
        if (ArrayUtils.isEmpty(projectTypeVOS)){
            return null;
        }
        SingleUpdateBpTemplate<ProjectTypeVO> bp = new SingleUpdateBpTemplate<>(mapper, UPDATE_STATUS);
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<ProjectTypeVO>());
        for (ProjectTypeVO p:projectTypeVOS){
            p.setPkTenant(RuntimeEnvironment.getTenantId());
            p.setEnableStatus((int) EnableStatusConst.DISABLE);
            //停用
            bp.update(p);
        }
        ProjectTypeVO[] projectTypeVOS1 = mapper.queryAllEnable(projectTypeVO.getScode(), projectTypeVO.getPkTenant(), EnableStatusConst.ALL);

        return projectTypeVOS1;
    }

    //判断父节点状态是否启动
    public boolean isEnable(ProjectTypeVO projectTypeVO){
        ProjectTypeVO enable = mapper.isEnable(projectTypeVO.getPkParent(), projectTypeVO.getPkTenant());
        if (enable!=null&&enable.getEnableStatus()==EnableStatusConst.ENABLE){
            return true;
        }
        return false;
    }

}
